package com.pgytesting.secondhandcar.service.impl;

import com.pgytesting.secondhandcar.model.AdministratorInformation;
import com.pgytesting.secondhandcar.dao.AdministratorInformationDao;
import com.pgytesting.secondhandcar.service.AdministratorInformationService;
import com.pgytesting.secondhandcar.util.SessionUtil;
import com.pgytesting.secondhandcar.util.UtilMethod;
import com.pgytesting.secondhandcar.vo.Parameters;
import com.pgytesting.secondhandcar.vo.ResponseMessage;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 管理员信息表(AdministratorInformation)表服务实现类
 *
 * @author makejava
 * @since 2019-08-15 10:07:13
 */
@Service("administratorInformationService")
public class AdministratorInformationServiceImpl implements AdministratorInformationService {
    @Resource
    private AdministratorInformationDao administratorInformationDao;

    /**
     * 通过ID查询单条数据
     *
     * @param
     * @return 实例对象
     */
    @Override
    public AdministratorInformation queryById(Integer id) {
        return this.administratorInformationDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<AdministratorInformation> queryAllByLimit(int offset, int limit) {
        return this.administratorInformationDao.queryAllByLimit(offset, limit);
    }
    
    /**
     * 条件查询多条数据
     *
     * @param administratorInformation 实例对象
     * @return 对象列表
     */
    @Override
    public List<AdministratorInformation> queryAll(AdministratorInformation administratorInformation) {
    	administratorInformation.setStatus(0);
        return this.administratorInformationDao.queryAll(administratorInformation);
    }

    /**
     * 新增数据
     *
     * @param administratorInformation 实例对象
     * @return 实例对象
     */
    @Override
    public AdministratorInformation insert(AdministratorInformation administratorInformation) {
        this.administratorInformationDao.insert(administratorInformation);
        return administratorInformation;
    }

    /**
     * 修改数据
     *
     * @param administratorInformation 实例对象
     * @return 实例对象
     */
    @Override
    public AdministratorInformation update(AdministratorInformation administratorInformation) {
        this.administratorInformationDao.update(administratorInformation);
        return this.queryById(administratorInformation.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.administratorInformationDao.deleteById(id) > 0;
    }

	@Override
	public AdministratorInformation queryObj(AdministratorInformation administratorInformation) {
		administratorInformation.setStatus(0);
		return administratorInformationDao.queryObj(administratorInformation);
	}

	@Override
	public ResponseMessage verificationLogin(AdministratorInformation administratorInformation) {
		try {
			AdministratorInformation ai = new AdministratorInformation();
			ai.setUserNumber(administratorInformation.getUserNumber());
			ai = this.queryObj(ai);
			if(ai == null) {
				return ResponseMessage.errorResponse("用户名不存在");
			}
			if(ai.getPassword().equals(UtilMethod.md5Encryption(administratorInformation.getPassword()))) {
				AdministratorInformation loginAdmin = new AdministratorInformation();
				loginAdmin.setId(ai.getId());
				loginAdmin.setUserNumber(ai.getUserNumber());
				SessionUtil.updateSession("user", loginAdmin);
				SessionUtil.updateSession("login", Parameters.USER_TYPE_ADMIN);
				return ResponseMessage.successResponse(loginAdmin);
			}else {
				return ResponseMessage.errorResponse("用户名或者密码不正确");
			}
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseMessage.errorResponse("管理员登录出现异常");
		}
	}
}